; Lijiang Video Compression Tool Installer Script
; Using NSIS 3.0+
; Note: Configuration values in this file should match those in config.py

!define APP_NAME "Lijiang Video Compression Tool"
!define APP_NAME_ZH "力江视频压缩工具"
!define APP_VERSION "1.0.0"
!define APP_PUBLISHER "Lijiang"
!define APP_EXE "LijiangVideoCompressionTool.exe"
!define APP_ICON "icon.ico"

; Include Modern UI
!include "MUI2.nsh"
!include "FileFunc.nsh"

; Installer Properties
Name "${APP_NAME}"
OutFile "LijiangVideoCompressionTool_Setup_v${APP_VERSION}.exe"
InstallDir "$PROGRAMFILES\Lijiang\视频压缩工具"
InstallDirRegKey HKLM "Software\Lijiang\视频压缩工具" "Install_Dir"
RequestExecutionLevel admin

; Interface Settings
!define MUI_ICON "${APP_ICON}"
!define MUI_UNICON "${APP_ICON}"
; Note: Commented out bitmap files that don't exist
; !define MUI_HEADERIMAGE
; !define MUI_HEADERIMAGE_BITMAP "header.bmp"
; !define MUI_WELCOMEFINISHPAGE_BITMAP "welcome.bmp"
; !define MUI_UNWELCOMEFINISHPAGE_BITMAP "welcome.bmp"

; Install Pages
!insertmacro MUI_PAGE_WELCOME
; Note: Commented out license page since LICENSE.txt doesn't exist
; !insertmacro MUI_PAGE_LICENSE "LICENSE.txt"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

; Uninstall Pages
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

; Language
!insertmacro MUI_LANGUAGE "SimpChinese"

; Install Section
Section "Main Program" SecMain
    SetOutPath "$INSTDIR"
    
    ; Copy main program files
    File "dist\${APP_EXE}"
    File "${APP_ICON}"
    
    ; Create uninstaller
    WriteUninstaller "$INSTDIR\Uninstall.exe"
    
    ; Write registry information
    WriteRegStr HKLM "Software\Lijiang\视频压缩工具" "Install_Dir" "$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "DisplayName" "${APP_NAME_ZH}"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "UninstallString" "$INSTDIR\Uninstall.exe"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "DisplayIcon" "$INSTDIR\${APP_ICON}"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "Publisher" "${APP_PUBLISHER}"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "DisplayVersion" "${APP_VERSION}"
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "NoModify" 1
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "NoRepair" 1
    
    ; Calculate installation size
    Call GetInstalledSize
    Pop $0
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool" "EstimatedSize" "$0"
SectionEnd

Section "Desktop Shortcut" SecDesktop
    CreateShortCut "$DESKTOP\${APP_NAME_ZH}.lnk" "$INSTDIR\${APP_EXE}" "" "$INSTDIR\${APP_ICON}"
SectionEnd

Section "Start Menu" SecStartMenu
    CreateDirectory "$SMPROGRAMS\Lijiang"
    CreateShortCut "$SMPROGRAMS\Lijiang\${APP_NAME_ZH}.lnk" "$INSTDIR\${APP_EXE}" "" "$INSTDIR\${APP_ICON}"
    CreateShortCut "$SMPROGRAMS\Lijiang\Uninstall ${APP_NAME_ZH}.lnk" "$INSTDIR\Uninstall.exe" "" "$INSTDIR\Uninstall.exe"
SectionEnd

; Uninstall Section
Section "Uninstall"
    ; Delete files
    Delete "$INSTDIR\${APP_EXE}"
    Delete "$INSTDIR\${APP_ICON}"
    Delete "$INSTDIR\Uninstall.exe"
    
    ; Delete directories
    RMDir "$INSTDIR"
    
    ; Delete shortcuts
    Delete "$DESKTOP\${APP_NAME_ZH}.lnk"
    Delete "$SMPROGRAMS\Lijiang\${APP_NAME_ZH}.lnk"
    Delete "$SMPROGRAMS\Lijiang\Uninstall ${APP_NAME_ZH}.lnk"
    RMDir "$SMPROGRAMS\Lijiang"
    
    ; Delete registry keys
    DeleteRegKey HKLM "Software\Lijiang\视频压缩工具"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LijiangVideoCompressionTool"
SectionEnd

; Function to calculate installation size
Function GetInstalledSize
    Push $0
    Push $1
    StrCpy $0 0
    ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $1
    IntFmt $0 "0x%08X" $0
    Pop $1
    Exch $0
FunctionEnd

; GetSize macro is already included at the top
